package com.cloud.wyf;

import com.cloud.wyf.Entity.*;
import com.cloud.wyf.Service.UserService;
import org.apache.commons.io.IOUtils;
import org.jodconverter.DocumentConverter;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.servlet.ServletOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@SpringBootTest
public class test {

    @Autowired
    private DocumentConverter converter;

    @Autowired
    UserService us;

   @Test
    public void test(){
        File file = new File("E:\\uploadFile\\吴俨锋.docx");//需要转换的文件
        try {
            File newFile = new File("E:\\testMyDoc");//转换之后文件生成的地址
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            String savePath="E:\\testMyDoc\\"; //pdf文件生成保存的路径
            String fileName="JCccc"+ UUID.randomUUID().toString().replaceAll("-","").substring(0,6);
            String fileType=".pdf"; //pdf文件后缀
            String newFileMix=savePath+fileName+fileType;  //将这三个拼接起来,就是我们最后生成文件保存的完整访问路径了

            //文件转化
            converter.convert(file).to(new File(newFileMix)).execute();
            //使用response,将pdf文件以流的方式发送的前端浏览器上
//            ServletOutputStream outputStream = response.getOutputStream();
//            InputStream in = new FileInputStream(new File(newFileMix));// 读取文件
//            int i = IOUtils.copy(in, outputStream);   // copy流数据,i为字节数
//            in.close();
//            outputStream.close();
            // System.out.println("流已关闭,可预览,该文件字节大小："+i);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    public void Mult(){
//        User user = us.selectUserById(uid);
//        List<Middel> list=new ArrayList<>();
//        List<Part> all = us.selectByDirectPart("机关党委");
//        String str="";
//        for(Part part:all){
//            str=str+part.getPart()+";";//得到所有属于机关党委的部门
//        }
//        if(user.getRole()==1){//领导
//            Leader leader = us.selectLeaderById(user.getId());
//            list = atainLeaderNeedVote(leader,str);
//        }
////        else if(user.getRole()==2){//中层干部
////            Middel middel = us.selectMidById(user.getId());
////            list=atainMiddelNeedVote(middel,str);
////        }
//        else if(user.getRole()==3){//群众
//            Ordinary ordinary = us.selectOrdinaryById(user.getId());
//            list=atainOrdinaryNeedVote(ordinary,str);
    }
//        }

    public List<Middel> atainLeaderNeedVote(Leader leader,String str){
        List<Middel> result=new ArrayList<>();
        List<Middel> allMiddel = us.selectAllMi();
        for(Middel m:allMiddel) {
            if(str.contains(m.getPart())){//属于机关党委
                if(m.getPosition().equals("正县")){
                    result.add(m);
                }else {
                    result.add(m);
                }
            }else {//不属于机关党委
                if(m.getPosition().equals("正县")){
                    result.add(m);
                }else {
                    if (leader.getManagepart()!=null&&!"".equals(leader.getManagepart())) {
                        if (leader.getManagepart().contains(m.getPart())) {
                            result.add(m);
                        }
                    }
                }
            }
        }
        return result;
    }

    public List<Middel>  atainOrdinaryNeedVote(Ordinary ordinary, String str){
        List<Middel> result=new ArrayList<>();
        List<Middel> allMiddel = us.selectAllMi();
        for(Middel m:allMiddel){
            if(str.contains(m.getPart())) {//属于机关党委
                if (str.contains(ordinary.getPart())) {//属于机关党委群众
                    result.add(m);
                }
            }else {//不属于机关党委
                String middelDirectPart = us.selectByPart(m.getPart()).getDirectparty();
                String ordinaryDirectPart = us.selectByPart(ordinary.getPart()).getDirectparty();
                if(ordinaryDirectPart.equals(middelDirectPart)){
                    result.add(m);
                }
            }
        }
        return result;
    }
}

